#!/bin/sh
echo this is dongle linux and andriod firmware build ...
fw_type=$1
app_path=$2
build_channel=$3
tool_path="/root/src/scripts/build_scripts/sign_apk_tool"

[ "$fw_type" != "android" -a "$fw_type" != "linux" ] && echo donot support other fw type,just only android or linux && exit 1
[ "x$app_path" == "x" ] && please input app ftp path && exit 1
echo ================build $fw_type=====================
echo download apk $app_path
echo app_path=$app_path > app_path_info
m_name=`echo $app_path  |awk -F "output/" '{print $2}' |awk -F "/" '{print $1}'` 
mkdir download_target
mkdir target
cur_path=`pwd`

if [ "$fw_type" == "android" ]; then
    build_lunch=$build_channel
    echo $build_lunch
    [ "$m_name" != "phoenix_app_dongle" ] && echo ftp path is not right && exit 1
    app_bc=`echo $app_path |awk -F "phoenix_app_dongle" '{print $2}' |awk -F "_" '{print $2"_"$3}'` #get apk path branch and commitid
    dir_name="`date "+%Y%m%d%H%M"`"_"$app_bc"
    ftp_dir=ftp://192.168.8.251/output/android_dongle_fw/$dir_name/
    curl -u aolingli:ALL1234!  $app_path > ftp_info
    curl -u aolingli:ALL1234! $ftp_dir/Emmc/ --ftp-create-dirs  #创建Emmc文件夹
    a_name=`cat ftp_info |grep .apk$ |grep release |awk -F " " '{print $4}'`
    echo apk_name=$a_name > build_channel
    channel=`cat build_channel |grep hpplay |awk -F "-" '{print $2}'`
    echo $channel
    echo apk_name=$a_name and module=$m_name
    echo ===========================download apk============================
    curl -u aolingli:ALL1234! -o download_target/$a_name  $app_path$a_name
    [ $? -ne 0 ] && echo download apk failed and exit && exit 1
    echo all download files as follow:
    ls ./download_target/
    tools_path=/home/lebo/hisi_dongle_firmware/dongle_android/HiSTBAndroidV600R003C01SPC031_mirror/Software/HiSTBAndroidV600R003C01SPC031
    cd $tools_path/
    echo =========== copy so and mdnsd files to android fw path==============
    cp -f $cur_path/download_target/*.apk $tools_path/device/hisilicon/bigfish/prebuilts/ThirdPartyApplications/system/hpplay.apk  #替换apk
    #cp -f $cur_path/download_target/*.apk ./out/target/product/Hi3798MV310/system/app/
    [ $channel = "happytest" ] && cp -f $tool_path/happytest/hpplay.lic $tools_path/device/hisilicon/Hi3798MV310/prebuilts/hpplay.lic  #替换hpplay.lic
    echo current path is `pwd`
    rm ./out/target/product/Hi3798MV310/Emmc/* -rf
    #rm out -rf
    echo =========================modify $channel ===========================
    modify_path=/home/lebo/hisi_dongle_firmware/dongle_android/HiSTBAndroidV600R003C01SPC031_mirror/Software/HiSTBAndroidV600R003C01SPC031/device/hisilicon
    if [ $channel = "fang_hua" ];then
          cp -f $tool_path/fang_hua/logo.png $modify_path/Hi3798MV310/prebuilts
          cp -f $tool_path/fang_hua/logo.img $modify_path/Hi3798MV310/prebuilts/telecom/
          grep -rn "CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0xff" $modify_path/bigfish/sdk/configs/hi3798mv310/hi3798mv31dms_hi3798mv310_android_cfg.mak
          sed -i "s/CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0xff/CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0x3a/g" $modify_path/bigfish/sdk/configs/hi3798mv310/hi3798mv31dms_hi3798mv310_android_cfg.mak
          flag_fit_fang_hua=`grep -rn "CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0x3a" $modify_path/bigfish/sdk/configs/hi3798mv310/hi3798mv31dms_hi3798mv310_android_cfg.mak`
          if [ "x$flag_fit_fang_hua" == "x" ] ;then
             echo  not find modify string  CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0x3a and exit
             exit 1
          else
             echo find modify string CFG_HI_POSITIVE_GPIO_LIGHT_NUMBER=0x3a as follow:
          fi
    fi
    echo ============= compile android dongle firmware ======================
    source build/envsetup.sh
    lunch $build_lunch
    make bigfish -j8
    echo =========== copy so and mdnsd files to android fw path==============
    echo $cur_path/download_target/*.apk
    cp -f $cur_path/download_target/*.apk ./out/target/product/Hi3798MV310/system/app/
    echo ============================copy Emmc to ftp========================
    Emmc_path=/home/lebo/hisi_dongle_firmware/dongle_android/HiSTBAndroidV600R003C01SPC031_mirror/Software/HiSTBAndroidV600R003C01SPC031/out/target/product/Hi3798MV310/Emmc/
    cd $Emmc_path
    for upload_file in `ls`
    do
        curl -u aolingli:ALL1234! -T "$upload_file"  $ftp_dir/Emmc/
        [ $? -ne 0 ] && echo upload files failed and exit  && exit 1
    done
    #cd $tools_path/out/target/product/Hi3798MV310/Emmc/
    #cp bootargs.bin fastboot.bin recovery.img update.zip $cur_path/target/
fi

if [ "$fw_type" == "linux" ] ;then
    [ "$m_name" != "ToB-RSDK" ] && echo ftp path is not right && exit 1
    echo module=$m_name
    app_bc=`echo $app_path |awk -F "ToB-RSDK" '{print $2}' |awk -F "_" '{print $2"_"$3}'` #get api path branch and commitid
    dir_name="`date "+%Y%m%d%H%M"`"_"$app_bc"
    ftp_dir=ftp://192.168.8.251/output/linux_dongle_fw/$dir_name/
    echo =================download so files and mdnsd====================
    app_path="$app_path"bin/linux/arm_hs320/
    curl -u aolingli:ALL1234!  $app_path > ftp_info
    cat ftp_info |grep .so$ |awk -F " " '{print $4}' > ftp_info_tmp
    cat ftp_info |grep mdnsd$ |awk -F " " '{print $4}' >> ftp_info_tmp
    for a_name in `cat ftp_info_tmp`
        do
	    echo  download file $a_name
            curl -u aolingli:ALL1234! -o download_target/$a_name  $app_path
            [ $? -ne 0 ] && echo download apk failed and exit && exit 1
        done
    echo all download files as follow:
    ls ./download_target/
    tools_path=/home/lebo/hisi_dongle_firmware/dongle_linux/HiSTBLinuxV100R005C00SPC061CP0003/Software/HiSTBLinuxV100R005C00SPC061CP0003_SmartCast/
    cd $tools_path/
    echo current path is `pwd`
    rm ./out/hi3716mv430/hi3716mv430_dmd_dongle/image/* -rf
    echo ================== compile: make build =================
   # source env.sh
   # make clean
   # make build -j8
    echo =========== copy so and mdnsd files to linux fw path==============
    cp -f   $cur_path/download_target/*.so ./out/hi3716mv430/hi3716mv430_dmd_dongle_loader/rootbox/usr/lib/
    cp -f  $cur_path/download_target/mdnsd ./out/hi3716mv430/hi3716mv430_dmd_dongle_loader/rootbox/usr/bin/
    echo ================= compile: make fs ================
    make fs
    #copy linux firmware result to target dir
    cp  ./out/hi3716mv430/hi3716mv430_dmd_dongle_loader/image/nand_image/* $cur_path/target/
fi

cd $cur_path
cp app_path_info target/
cd $cur_path/target/
echo =============== upload result files to ftp =====================
echo upload all files as follow:
ls
echo will create remmote path $ftp_dir and upload files to it
curl -u aolingli:ALL1234! $ftp_dir --ftp-create-dirs  #create remote dir
[ $? -ne 0 ] && echo create remote dir failed and exit  && exit 1
for upload_file in `ls`
do
    echo upload_file=$upload_file
    curl -u aolingli:ALL1234! -T "$upload_file"  $ftp_dir
    [ $? -ne 0 ] && echo upload files failed and exit  && exit 1
done
echo ftp_path=$ftp_dir
